package com.inspur.his.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.his.common.BaseController;
import com.inspur.his.pojo.Drugsstorrecord;
import com.inspur.his.pojo.Member;
import com.inspur.his.service.DrugsstorrecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;

//药品管理
@Controller
public class DrugsStorrecordController {
    //这里调用service
    @Autowired
    DrugsstorrecordService drugsstorrecordService;

    @RequestMapping("drugsstorrecordListController")
    public String drugsstorrecordList(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        String DRUGSID = request.getParameter("drugsId");
        String INDATE = request.getParameter("inDate");
        PageHelper.startPage(pn, 10);
        HashMap map = new HashMap();
        map.put("DRUGSID",String.valueOf(DRUGSID));
        map.put("STORAGEDATE",String.valueOf(INDATE));
        List<Drugsstorrecord> drugsstorrecordList = drugsstorrecordService.drugsstorrecordList(map);
        /*这里pageInfo.list就能获得分页完的数据*/
        PageInfo<Drugsstorrecord> pageInfo = new PageInfo<Drugsstorrecord>(drugsstorrecordList,drugsstorrecordList.size());
        /*Model主要用于传值给jsp*/
        model.addAttribute("drugsstorrecordList",drugsstorrecordList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("vdrugsid",DRUGSID);

        return "中西药房出入库系统/中药入库列表.jsp";
    }

    @RequestMapping("drugsstorrecordDateController")
    public String findByDate(HttpServletRequest request, Model model, @RequestParam(value = "pn", defaultValue = "1") Integer pn) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        PageHelper.startPage(pn, 5);
        HashMap map = new HashMap();
        List<Drugsstorrecord> drugsstorrecordList = drugsstorrecordService.findByDate(map);
        /*这里pageInfo.list就能获得分页完的数据*/
        PageInfo<Drugsstorrecord> pageInfo = new PageInfo<Drugsstorrecord>(drugsstorrecordList,drugsstorrecordList.size());
        /*Model主要用于传值给jsp*/
        model.addAttribute("drugsstorrecordList",drugsstorrecordList);
        model.addAttribute("pageInfo",pageInfo);
        return "中西药房出入库系统/中药入库列表.jsp";
    }


    @RequestMapping("DrugsStorFromController")//访问地址
    public String DrugsStorFrom(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //跳转到添加页面
        return "中西药房出入库系统/中药入库登记.jsp";
    }

    @RequestMapping("drugsstorrecordAddController")
    public String drugsstorrecordAdd(HttpServletRequest request, Model model) throws UnsupportedEncodingException{
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        //2.接受页面提交的数据
        String DRUGSID = request.getParameter("drugsId");
        String STANDARD = request.getParameter("standard");
        String UNIT = request.getParameter("unit");
        double PRICE = Double.parseDouble(request.getParameter("price"));
        int NUM = Integer.parseInt(request.getParameter("num"));
        String STORAGEDATE = request.getParameter("storageDate");
        String SUPPLYUNIT = request.getParameter("supplyUnit");
        String INVOICENO = request.getParameter("invoiceNo");
        Drugsstorrecord drugsstorrecord = new Drugsstorrecord();
        drugsstorrecord.setDRUGSID(DRUGSID);
        drugsstorrecord.setSTANDARD(STANDARD);
        drugsstorrecord.setUNIT(UNIT);
        drugsstorrecord.setPRICE(PRICE);
        drugsstorrecord.setNUM(NUM);
        drugsstorrecord.setSTORAGEDATE(STORAGEDATE);
        drugsstorrecord.setSUPPLYUNIT(SUPPLYUNIT);
        drugsstorrecord.setINVOICENO(INVOICENO);

        drugsstorrecordService.drugsstorrecordAdd(drugsstorrecord);
        return "redirect:drugsstorrecordListController";
    }

    @RequestMapping("drugsstorrecordDeleteController")
    public String drugsstorrecordDelete(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
        //1.设置字符编码集
        request.setCharacterEncoding("UTF-8");
        int id = Integer.parseInt(request.getParameter("id"));
        Drugsstorrecord drugsstorrecord = new Drugsstorrecord();
        drugsstorrecord.setID(id);
        Drugsstorrecord drugsstorrecord2 = drugsstorrecordService.findById(drugsstorrecord);
        drugsstorrecordService.drugsstorrecordDelete(drugsstorrecord2);
        return "redirect:drugsstorrecordListController";
    }
}
